var app = angular.module('associationSystem', ['ngFileUpload', 'ui.router', 'ui.bootstrap', 'ngAnimate', 'ui.tree', 'ng-pagination']);

app.run(function ($rootScope, $state, $stateParams) {
    $rootScope.state = $state;
    $rootScope.stateParams = $stateParams;
    $rootScope.param = {
        fieldName: "",
        value: ""
    };
});

// HttpInterceptor
app.factory('HttpInterceptor', function ($q, $injector) {
    return {
        request: function (config) {
            return config;
        },
        requestError: function (err) {
            return $q.reject(err);
        },
        response: function (res) {
            return res;
        },
        responseError: function (err) {
            var stateService = $injector.get('$state');
            if (-1 === err.status) {
            } else if (500 === err.status) {
                stateService.go('index.500');
            } else if (404 === err.status) {
                stateService.go('index.404');
            }
            return $q.reject(err);
        }
    };
});

// 页面间传值
app.factory('Data', function () {
    var savedData = {};

    function set(data) {
        savedData = data;
    }

    function get() {
        return savedData;
    }

    return {
        set: set,
        get: get
    }
})

// AngularJS trust html
    .filter('trustHtml', function ($sce) {
        return function (input) {
            return $sce.trustAsHtml(input);
        }
    })

// Route
app.config(function ($stateProvider, $urlRouterProvider, $locationProvider,
                     $httpProvider) {
    $httpProvider.interceptors.push('HttpInterceptor');
    $urlRouterProvider.otherwise('/index');
    $stateProvider
    //index
        .state('index', {
            url: '/index',
            views: {
                'home': {
                    templateUrl: 'html/main/home.html'
                },
                'navbar-side@index': {
                    templateUrl: 'html/main/navbar-side.html',
                    controller: NavbarSideCtrl
                },
                'navbar-header@index': {
                    templateUrl: 'html/main/navbar-header.html',
                    controller: NavbarHeaderCtrl
                },
                'navbar-footer@index': {
                    templateUrl: 'html/main/navbar-footer.html'
                },
                'content@index': {
                    templateUrl: 'html/main/content.html'
                }
            }
        })
        //404
        .state('index.404', {
            url: '/404',
            views: {
                'content@index': {
                    templateUrl: 'html/exception/404.html'
                }
            }
        })
        //500
        .state('index.500', {
            url: '/500',
            views: {
                'content@index': {
                    templateUrl: 'html/exception/500.html'
                }
            }
        })
        //all association
        .state('index.allAssociation', {
            url: '/allAssociation',
            views: {
                'content@index': {
                    templateUrl: 'html/allAssociation/index.html',
                    controller: AllAssociationCtrl
                }
            }
        })

        //association info
        .state('index.associationInfo', {
            url: '/association/:uid',
            views: {
                'content@index': {
                    templateUrl: 'html/allAssociation/association-info.html',
                    controller: AssociationInfoCtrl
                }
            }
        })

        //association apply
        .state('index.associationApply', {
            url: '/association/:uid/apply',
            views: {
                'content@index': {
                    templateUrl: 'html/allAssociation/association-apply.html',
                    controller: AssociationApplyCtrl
                }
            }
        })

        //my association
        .state('index.myAssociation', {
            url: '/myAssociation',
            views: {
                'content@index': {
                    templateUrl: 'html/myAssociation/index.html',
                    controller: MyAssociationCtrl
                }
            }
        })

        //create association
        .state('index.createAssociation', {
            url: '/createAssociation',
            views: {
                'content@index': {
                    templateUrl: 'html/createAssociation/index.html',
                    controller: CreateAssociationCtrl
                }
            }
        })

        //user info
        .state('index.userInfo', {
            url: '/userInfo',
            views: {
                'content@index': {
                    templateUrl: 'html/userInfo/index.html',
                    controller: UserInfoCtrl
                }
            }
        })

        //change password
        .state('index.userInfo.password', {
            url: '/password',
            views: {
                'content@index': {
                    templateUrl: 'html/userInfo/password-change.html',
                    controller: ChangePasswordCtrl
                }
            }
        })

        //approval
        .state('index.approval', {
            url: '/approval',
            views: {
                'content@index': {
                    templateUrl: 'html/approval/index.html',
                    controller: ApprovalCtrl
                }
            }
        })

        //manager
        .state('index.management', {
            url: '/management',
            views: {
                'content@index': {
                    templateUrl: 'html/management/index.html'
                }
            }
        })
        .state('index.management.manageSchoolManager', {
            url: '/manageSchoolManager',
            views: {
                'content@index': {
                    templateUrl: 'html/management/index.html'
                },
                'tab@index.management.manageSchoolManager': {
                    templateUrl: 'html/management/manage-school-manager.html',
                    controller: ManageSchoolManagerCtrl
                }
            }
        })
        .state('index.management.manageStudent', {
            url: '/manageStudent',
            views: {
                'content@index': {
                    templateUrl: 'html/management/index.html'
                },
                'tab@index.management.manageStudent': {
                    templateUrl: 'html/management/manage-student.html',
                    controller: ManageStudentCtrl
                }
            }
        })
        .state('index.management.addSchoolManager', {
            url: '/addSchoolManager',
            views: {
                'content@index': {
                    templateUrl: 'html/management/add-school-manager.html',
                    controller: ManageSchoolManagerCtrl
                }
            }
        })

        //create application
        .state('index.createApplication', {
            url: '/application/create',
            views: {
                'content@index': {
                    templateUrl: 'html/application/create.html',
                    controller: CreateApplicationCtrl
                }
            }
        })

        //create application
        .state('index.improveAssociation', {
            url: '/association/improve',
            params: {
                'uid': null,
                'name': null
            },
            views: {
                'content@index': {
                    templateUrl: 'html/manager/improve.html',
                    controller: AssociationImproveCtrl
                }
            }
        })

        .state('index.publishAnnouncement', {
            url: '/association/:uid/announcement/publish',
            views: {
                'content@index': {
                    templateUrl: 'html/manager/publish-announcement.html',
                    controller: PublishAnnouncementCtrl
                }
            }
        })

        .state('index.editAnnouncement', {
            url: '/association/:uid/announcement/edit',
            views: {
                'content@index': {
                    templateUrl: 'html/manager/edit-announcement.html',
                    controller: EditAnnouncementCtrl
                }
            }
        })
});